import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Demo1_8 {
    public static void main(String[] args) {
        //ArrayList的遍历
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(10);
        arrayList.add(12);
        arrayList.add(24);
        arrayList.add(58);
        arrayList.add(60);
        //方法1：for循环遍历
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.print(arrayList.get(i) + " ");
        }
        System.out.println();
        System.out.println("======");

        //方法2：foreach遍历
        for (Integer x : arrayList) {
            System.out.print(x + " ");
        }
        System.out.println();
        System.out.println("=======");

        //方法3：迭代器
        Iterator<Integer> it =arrayList.iterator();
        while(it.hasNext()) {
            System.out.print(it.next()+"");
        }
        System.out.println();
        System.out.println("=========");
    }

    public static void main2(String[] args) {
        //ArrayList的方法
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(10);
        arrayList.add(12);
        arrayList.add(24);
        arrayList.add(58);
        arrayList.add(60);
        System.out.println(arrayList);
        //截取部分元素
        List<Integer> list = arrayList.subList(1, 3);
        System.out.println(list);
        list.set(1, 25);
        System.out.println(list);
        System.out.println(arrayList);


    }

    public static void main1(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(10);
        arrayList.add(20);
        //一次性全部添加元素
        arrayList.addAll(arrayList);
        System.out.println(arrayList);
        //删除相应下标的元素
        arrayList.remove(1);
        System.out.println(arrayList);
        arrayList.remove(new Integer(10));
        arrayList.remove(Integer.valueOf(10));//进行拆箱再删除该元素
        System.out.println(arrayList);
    }


}
